package com.smart.manager.product.repository;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import com.smart.common.entities.constant.ArcoTips;
import com.smart.common.entities.core.Page;
import com.smart.common.entities.option.SelectOption;
import com.smart.manager.product.entities.body.RPropsSnacks;
import com.smart.manager.product.entities.enums.ProfileStatus;
import com.smart.manager.product.entities.models.PropsSnacks;
import com.smart.manager.product.entities.models.query.QPropsCombo;
import com.smart.manager.product.entities.models.query.QPropsSnacks;
import io.ebean.DB;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * 游戏管理  零食档案
 * @author leo
 * @date 2023/09/19
 */
@Service
@Log4j2
@RequiredArgsConstructor
public class PropsSnacksRepository {

    public Page<PropsSnacks> pageList(RPropsSnacks.SearchBody search) {
        QPropsSnacks query = new QPropsSnacks();
        Opt.ofBlankAble(search.getCode()).ifPresent(x -> query.code.contains(x));
        Opt.ofBlankAble(search.getName()).ifPresent(x -> query.name.contains(x));
        Opt.ofBlankAble(search.getStatus()).ifPresent(x -> query.status.eq(x));
        query.order().sort.asc();
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows());
        return new Page<PropsSnacks>().load(query.findPagedList());
    }

    public void add(RPropsSnacks.EditBody body) {
        QPropsSnacks query = new QPropsSnacks();
        PropsSnacks record = query.code.eq(body.getCode()).setMaxRows(1).findOne();
        Assert.isNull(record, "创建失败:, 零食代码已存在");
        BeanUtil.copyProperties(body, PropsSnacks.class).save();
    }


    public void update(Long id, RPropsSnacks.EditBody body) {
        QPropsSnacks query = new QPropsSnacks();
        PropsSnacks record = query.id.eq(id).setMaxRows(1).findOne();
        Assert.notNull(record, ArcoTips.UPDATE_NOT_FOUND);
        BeanUtil.copyProperties(body, record);
        Objects.requireNonNull(record).update();
    }

    public int delByIds(List<Long> ids) {
        List<PropsSnacks> list = new QPropsSnacks().id.in(ids).findList();
        Assert.notEmpty(list, ArcoTips.DELETE_NOT_FOUND);
        for (PropsSnacks item : list) {
            log.warn("判断数据是否能被删除");
        }
        return DB.deleteAll(list);
    }

    /**
     * 获取全部零食
     *
     * @return {@link List}<{@link PropsSnacks}>
     */
    public List<PropsSnacks> getList(){
        return new QPropsSnacks().status.eq(ProfileStatus.ON).findList();
    }
    /**
     * 获取数据字典下拉选项
     * @return {@code List<SelectOption>}
     */
    public List<SelectOption> getOptions() {
        QPropsSnacks place = new QPropsSnacks();
        return place.findStream()
                .map(x -> new SelectOption().setLabel(x.getName()).setValue(x.getCode()).setDisabled(false))
                .collect(Collectors.toList());
    }


}






